<template>
	<view class="container">
		<uni-forms ref="form">
			<uni-forms-item label="内容" name="content">
				<uni-easyinput v-model="data.content" type="textarea" autoHeight placeholder="请输入留言内容" />
			</uni-forms-item>
			<uni-forms-item>
				<button type="primary" @click="add()">发表</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: {
					"content": "",
					"date": "",
					"id": 0,
					"name": "",
					"title": "留言"
				}
			}
		},
		onLoad() {
			let userinfo = uni.getStorageSync('userinfo');
			if (userinfo) {
				this.data.name = userinfo.name;
			}
		},
		methods: {
			add() {
				this.data.date = new Date().toISOString();
				uni.request({
					url: 'http://localhost:8090/freshmenapp/message/addone',
					method: 'POST',
					data: this.data,
					success: (res) => {
						if (res.data.code === 200) {
							this.data.content = '';
						} else {
							uni.showToast({
								title: '留言成功',
								duration: 2000
							});
						}
					},
					fail: (err) => {
						console.error(err);
					}
				});
			}
		}
	}
</script>


<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>